package com.breaktian.assemble.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by breaktian on 2016/8/22.
 * TableAdapter 表格布局的adapter可以方便使用表格布局
 */
abstract public class TableAdapter {

    private Context mContext;
    private List<? extends Object> mItems;
    private TableLayout mLayout;
    private int mColumnCount;
    private LayoutInflater inflater;
    private ItemClickListener listener;
    private List<View> views = new ArrayList<View>();


    public TableAdapter(int columnCount, TableLayout layout,
                        Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        mLayout = layout;
        mColumnCount = columnCount;

    }

    public TableAdapter(int columnCount, TableLayout layout, List<? extends Object> items,
                        Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        mItems = items;
        mLayout = layout;
        mColumnCount = columnCount;
        if (mItems == null)
            return;
        initLayout(columnCount, layout, items, context);

    }


    public void setData(List<? extends Object> items) {
        mItems = items;
        initLayout(mColumnCount, mLayout, mItems, mContext);
    }

    public List<? extends Object> getData(){
        return mItems;
    }

    public List<View> getViews() {
        return views;
    }

    public void addViews(View v) {
        views.add(v);
    }

    private void initLayout(int columnCount, TableLayout layout, List<? extends Object> items,
                            Context context) {
        layout.removeAllViews();
        int count = items.size();

        int rowCount = count / columnCount;
        if (count % columnCount != 0)
            rowCount = rowCount + 1;

        TableRow row = null;
        ViewGroup.LayoutParams rowParam = null;
        for (int i = 0; i < rowCount * columnCount; i++) {
            TableRow.LayoutParams itemParam = new TableRow.LayoutParams(0,
                    TableRow.LayoutParams.WRAP_CONTENT);
            itemParam.weight = 1;

            if (i % columnCount == 0) {
                row = new TableRow(context);
                rowParam = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                layout.addView(row, rowParam);
            }

            View itemView = null;

            if (i >= count) {
                itemView = new TextView(context);
                itemView.setVisibility(View.INVISIBLE);
            } else {
                Object itemObj = mItems.get(i);
                itemView = getItemView(context, itemObj, i);
            }
            row.addView(itemView, itemParam);
        }
    }

    /**
     * 抽象方法 业务在此处写
     */
    public abstract View getItemView(final Context context, final Object itemObj, final int position);

    public void setOnItemClick(ItemClickListener listener) {
        this.listener = listener;
    };

    public interface ItemClickListener {
        void onClick(Object o, int pos, View v);

        void onLongClick(Object o, int pos, View v);
    }
}
